package com.yuntongxun.ecdemo.voicechat.ui.services;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;

import com.loopj.android.http.RequestParams;
import com.yuntongxun.ecdemo.common.utils.LogUtil;
import com.yuntongxun.ecdemo.voicechat.api.ApiConstants;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.Contants;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;
import com.yuntongxun.ecdemo.voicechat.presenter.ICommonRequestPresenter;
import com.yuntongxun.ecdemo.voicechat.presenter.impl.CommonRequestPresenterImpl;
import com.yuntongxun.ecdemo.voicechat.util.NetUtils;
import com.yuntongxun.ecdemo.voicechat.util.PreferenceUtils;
import com.yuntongxun.ecdemo.voicechat.util.iview.ICommonViewUi;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/12/26.
 */

public class OnlineService extends Service implements ICommonViewUi {

    private String onlineState=Contants.AVAILABLE;
    private Handler mHandler;
    private ICommonRequestPresenter iCommonRequestPresenter;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);

    }
    Runnable runnable;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        if (mHandler==null){
            mHandler = new Handler();
            onlineState=PreferenceUtils.getPrefString(this, Contants.Preference.ONLINE_STATE,Contants.AVAILABLE);

            iCommonRequestPresenter = new CommonRequestPresenterImpl(this, this);
            runnable=new Runnable() {
                @Override
                public void run()
                {

                    if (mHandler!=null){
                        toRequest(ApiConstants.EventTags.BEGIN_EVENT);
                        mHandler.postDelayed(this, 10000);
                    }

                }
            };
            mHandler.post(runnable);
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public void onEvent(PostResult postResult){
        switch (postResult.getTag()){
            case EventBusTags.AVAILABLE:
                onlineState=Contants.AVAILABLE;
                break;
            case EventBusTags.BUSY:
                onlineState=Contants.BUSY;
                break;
            case EventBusTags.LOGOUT:
                LogUtil.e("logout");
                mHandler.removeCallbacks(runnable);
                mHandler=null;
                stopSelf();
                break;
        }
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //2=在线，1＝忙碌
    @Override
    public void toRequest(int eventTag) {

        if (!NetUtils.isNetworkAvailable(this)) {
            return;
        }

        RequestParams map = new RequestParams();
        if (onlineState.equals(Contants.AVAILABLE)){
            map.put("online", "2");
        }else{
            map.put("online", "1");
        }
        iCommonRequestPresenter.request(eventTag, this, ApiConstants.Urls.ACCOUNT_ONLINE, map);
    }

    @Override
    public void getRequestData(int eventTag, String result) {

    }

    @Override
    public void onRequestSuccessException(int eventTag, String msg) {

    }

    @Override
    public void onRequestFailureException(int eventTag, String msg) {

    }

    @Override
    public void isRequesting(int eventTag, boolean status) {

    }
}
